package com.example.lenovo.weiyi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.SeeAllCSM;
import com.com.ruanmeng.utils.Params;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.com.ruanmeng.view.MyListView;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * 做过题的话  进题目列表页面
 */
public class SeeAllCeShiActivity extends BaseActivity {

    @BindView(R.id.rlist_seeallcs)
    MyListView rlistSeeallcs;
    @BindView(R.id.refresh_seeallcs)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.tv_seeall_jjz)
    TextView tvSeeallJjz;
    @BindView(R.id.tv_seeall_target)
    TextView tvSeeallTarget;
    @BindView(R.id.li_mubiao)
    LinearLayout liMubiao;
    ArrayList<SeeAllCSM.RowsBean> Temp_list = new ArrayList<>();
    private ListAdapter adapter;
    private SeeAllCSM model;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_see_all_ce_shi);
        ButterKnife.bind(this);
        ChangLayTitle("调查");
        AddActivity(this);
        LayBack();
        init();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Temp_list.size() == 0) {
            getData(true);
        } else {
            getData(false);
        }

    }

    public void init() {
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        adapter = new ListAdapter(Temp_list);
        rlistSeeallcs.setAdapter(adapter);
        rlistSeeallcs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                CommonUtil.showToask(SeeAllCeShiActivity.this, "dlick==" + position);
                if (Params.assessStatus != 2) {//==2 的时候已经评估过了  就不能再修改了  所以不让再进列表
                    Intent in = new Intent(SeeAllCeShiActivity.this, SurveyActivity.class);
                    in.putExtra("index", position + "");
                    in.putExtra("id", getIntent().getStringExtra("id"));
                    startActivity(in);
                }
            }
        });
    }

    /**
     * 获取题目信息
     */
    public void getData(Boolean isshowload) {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.TiMuList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(SeeAllCeShiActivity.this, "uid"));
        mRequest.add("assessId", getIntent().getStringExtra("id"));
        mRequest.add("isPager", "1");
        // 添加到请求队列
        CallServer.getRequestInstance().add(SeeAllCeShiActivity.this, 0, mRequest,
                new CustomHttpListener(SeeAllCeShiActivity.this, true, SeeAllCSM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        model = ((SeeAllCSM) data);
                        Temp_list.clear();
                        Temp_list.addAll(model.getRows());
                        adapter.notifyDataSetChanged();
                        /**
                         * 判断是否没有选择疼痛位置
                         * 没有的话就没有做题（调查）只有禁忌症
                         */
                        SeeAllCSM.RowsBean choose_list = Temp_list.get(0);
                        int a=0;
                        for (int i = 0; i < choose_list.getChoices().size(); i++) {
                            if(choose_list.getChoices().get(i).getIsSelect()==1){
                                a++;
                            }
                        }
                        if(a==0){
                            rlistSeeallcs.setVisibility(View.GONE);
                        }
                        //禁忌症Params.JJZ
                        if (!TextUtils.isEmpty(Params.JJZ)) {
                            tvSeeallJjz.setText(Params.JJZ);
                        } else {
                            tvSeeallJjz.setText("无");
                        }
                        if (!TextUtils.isEmpty(Params.MuBiao)) {
                            liMubiao.setVisibility(View.VISIBLE);
                            tvSeeallTarget.setText(Params.MuBiao);
                        } else {
                            liMubiao.setVisibility(View.GONE);
                        }

                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, isshowload);
    }

    /**
     * 选项列表
     */
    class TiMuAdapter extends BaseAdapter {
        private List<SeeAllCSM.RowsBean.ChoicesBean> list;

        public TiMuAdapter(List<SeeAllCSM.RowsBean.ChoicesBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_seeallanswer, null);
            TextView tv_xuanxiang = (TextView) v.findViewById(R.id.tv_seeall_xuanxiang);
            View view_ban = (View) v.findViewById(R.id.line_seeall_padding);
            View view_man = (View) v.findViewById(R.id.line_seeall_match);
            tv_xuanxiang.setText(list.get(position).getChoice() + "  " + list.get(position).getChoiceContent());
            if (position == list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.GONE);
            } else {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.GONE);
            }


            return v;
        }

    }

    /**
     * 题目列表
     */
    class ListAdapter extends BaseAdapter {
        private List<SeeAllCSM.RowsBean> list;

        public ListAdapter(List<SeeAllCSM.RowsBean> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View v, ViewGroup parent) {
            v = getLayoutInflater().inflate(R.layout.item_seeallcs, null);
            TextView title = (TextView) v.findViewById(R.id.tv_seeallcs_title);
            MyListView mlist_answer = (MyListView) v.findViewById(R.id.mlist_seeallcs_tm);
            View view_ban = (View) v.findViewById(R.id.line_seeallcspadding);
            View view_man = (View) v.findViewById(R.id.line_seeallcsmatch);
            ArrayList<SeeAllCSM.RowsBean.ChoicesBean> Temp_answerlist = new ArrayList<>();
            ArrayList<SeeAllCSM.RowsBean.ChoicesBean> Temp_answerSelectlist = new ArrayList<>();
            Temp_answerlist.addAll(Temp_list.get(position).getChoices());
            for (int i = 0; i < Temp_answerlist.size(); i++) {
                if (Temp_answerlist.get(i).getIsSelect() == 1) {
                    Temp_answerSelectlist.add(Temp_answerlist.get(i));
                }
            }
            TiMuAdapter tmuadapter = new TiMuAdapter(Temp_answerSelectlist);
            mlist_answer.setAdapter(tmuadapter);
            title.setText((position + 1) + "." + list.get(position).getTiMuContent());
            if (position == Temp_list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.GONE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
            mlist_answer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int arg2, long l) {
                    if (Params.assessStatus != 2) {//==2 的时候已经评估过了  就不能再修改了  所以不让再进列表
                        Intent in = new Intent(SeeAllCeShiActivity.this, SurveyActivity.class);
                        in.putExtra("index", position + "");
                        in.putExtra("id", getIntent().getStringExtra("id"));
                        startActivity(in);
                    }
                }
            });
            return v;
        }

    }

    public void doClick(View v) {
        Intent in;
        switch (v.getId()) {
            /*case R.id.li_jjz://禁忌症
                in = new Intent(SeeAllCeShiActivity.this, JinJiSurveyActivity.class);
                in.putExtra("id", getIntent().getStringExtra("id"));
                startActivity(in);
                break;
            case R.id.li_mubiao://期待目标
                in = new Intent(SeeAllCeShiActivity.this, JinJiSurveyActivity.class);
                in.putExtra("id", getIntent().getStringExtra("id"));
                startActivity(in);
                break;*/
        }
    }
}
